package com.itlzh.mall.common.handler;

import com.itlzh.mall.common.enums.ResponseEnum;
import com.itlzh.mall.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public Result handleException(Exception e) {
        log.error("Exception: {}", e.getMessage());
        return Result.fail(1000,e.getMessage());
    }

    /**
     * 参数类型错误
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
    public Result httpMediaTypeNotSupported(HttpMediaTypeNotSupportedException e) {
        log.error(e.getMessage(),e);//打印异常
        return Result.fail(ResponseEnum.PARAM_TYPE_BIND_ERROR);
    }


}
